<?php
class RepliesController extends AppController {

	var $name = 'Replies';

	function index() {
		$this->Reply->recursive = 0;
		$this->set('replies', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid reply', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('reply', $this->Reply->read(null, $id));
	}

	function add($id = null) {
		if (!empty($this->data)) {
			$this->Reply->create();
			//$this->data['Reply']['post_id'] = $id;
			//$this->data['Reply']['user_id'] = 1;
			//$this->Reply->set(array( 'post_id' => $_POST['post_id'], 
			//						'user_id' => $_POST['user_id'],
			//						'noidung' => $_POST['noidung'],
			//));
			if ($this->Reply->save($this->data)) {
				//$this->Session->setFlash(__('The reply has been saved', true));
				$this->redirect(array('controller' => 'posts', 'action' => 'view', $this->data['Reply']['post_id']));
			} else {
				$this->Session->setFlash(__('The reply could not be saved. Please, try again.', true));
				//$this->redirect(array('controller' => 'post', 'action' => 'tinvip'));
			}
		}
		$posts = $this->Reply->Post->find('list');
		$users = $this->Reply->User->find('list');
		$this->set(compact('posts', 'users'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid reply', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Reply->save($this->data)) {
				$this->Session->setFlash(__('The reply has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The reply could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Reply->read(null, $id);
		}
		$posts = $this->Reply->Post->find('list');
		$users = $this->Reply->User->find('list');
		$this->set(compact('posts', 'users'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for reply', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Reply->delete($id)) {
			$this->Session->setFlash(__('Reply deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Reply was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
